//
//  LTLButtonXiala.m
//  WeiBo
//
//  Created by LiTaiLiang on 16/9/22.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLButtonXiala.h"

@interface LTLButtonXiala ()

@property(nonatomic , weak) UIButton *button;

@property(nonatomic , strong) UIImageView *table;

@end

@implementation LTLButtonXiala
/**
 *  创建遮罩视图对象方法
 *
 *  @param frame 遮罩视图大小
 *  @param piont 显示视图中心点
 *  @param View  显示视图
 *
 *  @return 遮罩视图
 */
-(instancetype)initWithFrame:(CGRect)frame Button : (UIButton *) Button View : (UITableView *)View
{
    if ((self                       = [super initWithFrame:frame]) != nil) {
        //遮罩视图为无色
        self.backgroundColor            = [UIColor clearColor];
        //显示视图背景
         UIImageView *table              = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"popover_background"]];
        table.highly = 165;
        
        table.width = 200;
        
        View.frame                      = CGRectMake(table.bounds.origin.x+10, table.bounds.origin.y+17, table.bounds.size.width-20, table.bounds.size.height-20);
        //打开用户交互性
        table.userInteractionEnabled = YES;
        [table addSubview:View];

        [self addSubview:table];
        
        self.table                      = table;

    }
    return self;
}
/**
 *  创建遮罩视图类方法
 *
 *  @param piont 显示视图中心点
 *  @param View  显示视图
 *
 *  @return 遮罩视图
 */
+(instancetype)initButtonPiont : (UIButton *) Button View : (UITableView *)View
{

    return [[self alloc]initWithFrame:[UIScreen mainScreen].bounds Button:Button View :View];
}
//单击遮罩视图时移除遮罩视图
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //动画按钮图片
    [UIView animateWithDuration:0.1 animations:^{

 //       self.button.imageView.transform = CGAffineTransformIdentity;
    [_button setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
        self.table.alpha                = 0;
    }];
    //移除遮罩视图
    [self removeFromSuperview];
}
/**
 *  显示遮罩视图
 *
 *  @param button 按钮
 */
-(void)xianShiButton:(UIButton *)button
{
    self.button                     = button;
    //旋转按钮图片动画
    UIWindow *win                   = [UIApplication sharedApplication].keyWindow;
    
    CGRect newFream = [button convertRect:button.bounds toView:win];
    
    self.table.centerX = CGRectGetMidX(newFream);
    
    self.table.y = CGRectGetMaxY(newFream);
    
    //显示遮罩视图
    [win addSubview:self];
    [button setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];

//    button.imageView.image = [UIImage imageNamed:@"navigationbar_arrow_up"];
 
    self.table.alpha                = 1;

}
@end
